﻿using System;
using System.Web;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace BrnMall.Sms
{
    /// <summary>
    /// 短信验证码，使用同一个签名，对同一个手机号码发送短信验证码，允许每分钟1条，累计每小时7条。 短信通知，使用同一签名、同一模板，对同一手机号发送短信通知，允许每天50条（自然日）。
    /// </summary>
    public class AliSMS
    {
        /// <summary>
        /// 淘宝开放平台统一url
        /// </summary>
        private const String URL = "http://gw.api.taobao.com/router/rest";

        /// <summary>
        /// 淘宝开放平台apkey
        /// </summary>
        private const String APP_KEY = "23328418";

        /// <summary>
        /// 淘宝开放平台APP_SECRET
        /// </summary>
        private const String APP_SECRET = "d10d87ca31cb4bac3d75fa67878babb1";

        /// <summary>
        /// 产品
        /// </summary>
        private const String PRODUCT = "小程序";


        /// <summary>
        /// 该签名需后台配置,不可随便更改
        /// </summary>
        private const String SIGN_REGIST = "注册验证";
        private const String SIGN_LOGIN = "登录验证";
        private const String SIGN_CHANGE = "变更验证";

        /// <summary>
        /// 用户注册验证码
        /// 模板内容:验证码${code}，您正在注册成为${product}用户，感谢您的支持！
        /// </summary>
        private const String TEMPLATE_REGIST = "SMS_6265102";

        /// <summary>
        /// 登录确认验证码
        /// 模板内容:验证码${code}，您正在登录${product}，若非本人操作，请勿泄露。
        /// </summary>
        private const String TEMPLATE_LOGIN = "SMS_6265104";

        /// <summary>
        /// 信息变更验证码
        /// 模板内容:验证码${code}，您正在尝试变更${product}重要信息，请妥善保管账户信息。
        /// </summary>
        private const String TEMPLATE_CHANGE = "SMS_6265099";

        /// <summary>
        /// 发送验证码短信
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="code">验证码</param>
        /// <param name="signName">签名</param>
        /// <param name="templateCode">模板号</param>
        /// <returns></returns>
        private static bool Send(String phone, String code, String signName, String templateCode)
        {
            ITopClient client = new DefaultTopClient(URL, APP_KEY, APP_SECRET);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.SmsType = "normal";
            req.SmsFreeSignName = signName;
            req.SmsParam = "{\"code\":\"" + code + "\",\"product\":\"" + PRODUCT + "\"}";
            req.RecNum = phone;

            //短信模板.后台有说明
            req.SmsTemplateCode = templateCode;
            AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
            String rs = rsp.Body;
            
            if (rs.IndexOf("<success>true</success>") != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 注册时短信验证
        /// </summary>
        /// <param name="code">验证码</param>
        public static bool SendRegistCode(String phone, String code)
        {
            return Send(phone, code, SIGN_REGIST, TEMPLATE_REGIST);
        }   

        /// <summary>
        /// 登录时短信验证
        /// </summary>
        /// <param name="code">验证码</param>
        public static bool SendLoginCode(String phone, String code)
        {
            return Send(phone, code, SIGN_LOGIN, TEMPLATE_LOGIN);
        }

        /// <summary>
        /// 信息变更验证码
        /// </summary>
        /// <param name="code">验证码</param>
        public static bool SendModifyCode(String phone, String code)
        {
            return Send(phone, code, SIGN_CHANGE, TEMPLATE_CHANGE);
        }

    }
}